package com.lzh.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.io.IOException;

/**
 * @author zhehen.lu
 * @date 2025/8/13 10:02
 */
public class JacksonStringToLocalDateStringSerializer extends JsonSerializer<String> {
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd";

    public JacksonStringToLocalDateStringSerializer() {
    }

    @Override
    public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (value == null) {
            jsonGenerator.writeString("");
        } else {
            DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");

            try {
                LocalDate localDate = fmt.parseLocalDateTime(value).toLocalDate();
                jsonGenerator.writeString(localDate.toString());
            } catch (Exception e) {
            }
        }

    }
}
